import sys

class CodeBlock:
    def __init__(self, start: str = "{", end: str = "}", parent: 'CodeBlock' = None, star_sep: str= '\n', end_sep: str = '\n', sep: str = "\n", ic: int = 4):
        self.blockList = []
        self.start = start
        self.end = end
        self.sep = sep
        self.start_sep = star_sep
        self.end_sep = end_sep
        self.forbit_indent = False
        if parent is None:
            self.start_indent = 0
            self.indent_count = ic
        else:
            self.start_indent = parent.start_indent + 1
            self.indent_count = parent.indent_count
            parent.add_block(self)

    def set_forbit_indent(self, forbit:bool):
        self.forbit_indent = forbit

    def set_sep(self, sep: str):
        self.sep = sep

    def set_start_sep(self, star_sep: str= '\n', end_sep: str = '\n'):
        self.start_sep = star_sep
        self.end_sep = end_sep

    def __str__(self) -> str:
        return self.generate()

    def generate(self, indent_level: int = None) -> str:
        """
        生成代码字符串
        :param indent_level: 当前缩进级别（内部使用）
        :return: 格式化后的代码字符串
        """
        if indent_level is None:
            indent_level = self.start_indent
            
        indent_str = ' ' * self.indent_count * indent_level
        inner_indent = indent_level + 1 if not self.forbit_indent else indent_level
        inner_indent_str = ' ' * self.indent_count * inner_indent
        
        lines = []
        # 添加内容
        for item in self.blockList:
            if isinstance(item, str):
                lines.append(f"{inner_indent_str}{item}")
            elif isinstance(item, CodeBlock):
                lines.append(item.generate(inner_indent))
            else:
                lines.append(f"{inner_indent_str}{str(item)}")
        inner = f'{self.sep}'.join(lines)
        return f"{indent_str}{self.start}{self.start_sep}{inner}{self.end_sep}{indent_str}{self.end}"
    
    def __iadd__(self, other):
        """支持 += 运算符添加内容"""
        if isinstance(other, str):
            self.add_line(other)
        elif isinstance(other, CodeBlock):
            self.add_block(other)
        elif isinstance(other, list):
            for item in other:
                self += item
        return self
    
    def format_string(self, fmt: str, *args, **kwargs) -> str:
        if args:
            return fmt.format(*args)
        if kwargs:
            return fmt.format(**kwargs)
        return fmt
    
    def add_line(self, line: str):
        """
        添加一行代码
        :param line: 代码字符串
        """
        self.blockList.append(line)
        
    def add_block(self, block: 'CodeBlock', prefix: str = None, postfix: str = None):
        """
        添加一个子代码块
        :param block: CodeBlock 实例
        """
        # 调整子块的缩进级别
        block.adjust_indent(self.start_indent + 1 if not self.forbit_indent else self.start_indent, self.indent_count)
        if prefix is not None:
            block.start = f"{prefix}{block.start}"
        if postfix is not None:
            block.end = f"{block.end}{postfix}"
        self.blockList.append(block)
        
    def add_lines(self, lines: list):
        """
        添加多行代码
        :param lines: 可变数量的代码字符串
        """
        for line in lines:
            self.add_line(line)
            
    def add_comment(self, comment: str, style: str = "//"):
        """
        添加注释行
        :param comment: 注释内容
        :param style: 注释样式（默认为 "//")
        """
        self.add_line(f"{style} {comment}")

    def add_blank_line(self, count: int = 1):
        """
        添加空行
        :param count: 空行数量
        """
        for _ in range(count):
            self.add_line("")
            
    def adjust_indent(self, new_indent: int, indent_count: int = 4):
        """
        调整整个代码块的缩进级别
        :param new_indent: 新的缩进级别
        """
        self.start_indent = new_indent
        self.indent_count = indent_count
        
        # 递归调整所有子块的缩进
        for item in self.blockList:
            if isinstance(item, CodeBlock):
                item.adjust_indent(new_indent + 1 if not self.forbit_indent else new_indent, self.indent_count)

class LineCodeBlock(CodeBlock):
    def __init__(self, start = "", end = "", parent = None, star_sep = '', end_sep = '', sep = ",", ic = 0):
        super().__init__(start, end, parent, star_sep, end_sep, sep, ic)
        self.start_indent = 0

    def generate(self, indent_level: int = None) -> str:
        """
        生成代码字符串
        :param indent_level: 当前缩进级别（内部使用）
        :return: 格式化后的代码字符串
        """
        if indent_level is None:
            indent_level = self.start_indent
            
        indent_str = ' ' * self.indent_count * indent_level
        inner_indent = indent_level + 1 if not self.forbit_indent else indent_level
        inner_indent_str = ' ' * self.indent_count * inner_indent
        
        lines = []
        # 添加内容
        for item in self.blockList:
            if isinstance(item, str):
                lines.append(f"{inner_indent_str}{item}")
            elif isinstance(item, CodeBlock):
                lines.append(item.generate(inner_indent))
            else:
                lines.append(f"{inner_indent_str}{str(item)}")
        inner = f'{self.sep}'.join(lines)
        return f"{indent_str}{self.start}{self.start_sep}{inner}{self.end_sep}{self.end}"